﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 算法基本功.算法._0_数组._1_双指针.左右指针
{
    internal class 二分搜索直接找答案
    {
        public int Binary_Serach(int[] nums,int target) { 
            int left =0,right =nums.Length-1;

            while (left <= right) { 
                
                int mid = left + (right - left)/2;
                if (nums[mid] == target) {
                    return mid;
                } else if (nums[mid] < target) {
                    left = mid + 1;
                } else if (nums[mid] > target) { 
                    right = mid - 1;
                }
            }

            //啥都没找到
            return -1;
        }
    }
}
